
import request from "../../../../utils/request"

Page({
  /**
   * 页面的初始数据
   */
  data: {
    provinces: undefined,// 省份数据数组
    pIndex: 0,//选择的省下标
    cities: undefined,// 城市数据数组
    cIndex: 0,//选择的市下标
    areas: undefined,// 区县数数组
    aIndex: 0,//选择的区下标
    name: null,
    phone: null,
    detailsite: null,
    siteinfo: ""
  },

  getprovinces(e) {
    if (e) {
      // console.log("!")
      // console.log(e.detail.value)
      // 为了防止bug，在每次用户选择的时候清空，id
      this.setData({
        pIndex: e.detail.value,
        cIndex: 0,
        aIndex: 0
      })
      this.getchild()
      return
    }
    var provinces = this.data.provinces
    request(`/common/region/v2/province`).then(res => {
      // console.log(res)
      const provinces = [
        {
          id: 0,
          name: '请选择'
        }
      ].concat(res.data.data)
      let pIndex = 0
      this.setData({
        pIndex,
        provinces: provinces
      })
    })
  },
  getchild(e) {
    var provinces = this.data.provinces
    var pIndex = this.data.pIndex
    var pid = provinces[pIndex].id
    if (e) {
      // 为了防止bug，在每次用户选择的时候清空，id
      this.setData({
        cIndex: e.detail.value,
        aIndex: 0
      })
      this.getchildxiaji()
      return
    }
    if (this.data.pIndex > 0) {
      request(`/common/region/v2/child`, {
        pid: pid,
      }, "GET").then(res => {
        const cities = [
          {
            id: 0,
            name: "请选择"
          }
        ].concat(res.data.data)
        this.setData({
          cities: cities
        })
      })
    }
  },
  getchildxiaji(e) {
    if (e) {
      // console.log(e.detail.value)
      this.setData({
        aIndex: e.detail.value
      })
      // console.log(this.data.aIndex)
      return
    }
    var cities = this.data.cities
    var cIndex = this.data.cIndex
    var cid = cities[cIndex].id
    // console.log(cid)
    request('/common/region/v2/child', {
      pid: cid
    }, "GET").then(res => {
      // console.log(res.data.data)
      const areas = [
        {
          id: 0,
          name: "请选择"
        }
      ].concat(res.data.data)
      let aIndex = 0
      this.setData({
        aIndex,
        areas: areas
      })
    })
  },
  tijiao() {

    if (this.data.pIndex == 0 && this.data.aIndex == 0 && this.data.cIndex == 0) {
      wx.showModal({
        title: "提示",
        content: "请填写详细的地址",
        showCancel: false
      })
      return
    }
    if (this.data.name == null && this.data.detailsite) {
      wx.showModal({
        title: "提示",
        content: "信息不完整",
        showCancel: false
      })
      return
    }
    ///手机号码验证：
    if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(this.data.phone))) {
      wx.showModal({
        title: "提示",
        content: "手机号格式有误",
        showCancel: false
      })
      return
    }
    const sitedata = {
      address: this.data.detailsite,
      cityId: this.data.cities[this.data.cIndex].id,
      linkMan: this.data.name,
      mobile: this.data.phone,
      provinceId: this.data.provinces[this.data.pIndex].id,
      distrctId: this.data.areas[this.data.aIndex].id,
      token: wx.getStorageSync("token")
    }
    console.log(sitedata)
    request(`/weiinng/user/shipping-address/add`, sitedata, 'POST').then(res => {
      if (res.code == 0) {
        wx.navigateBack()
      } else {
        wx.showModal({
          title: "提示",
          content: "出现了问题请联系管理员",
          showCancel: false
        })
      }
    })
    // console.log(sitedata)
    console.log("提交！")
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getprovinces()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})